package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.POJO.Permission;
import com.itheima.POJO.Role;
import com.itheima.POJO.User;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

@Transactional
@Service(interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    public User findByUsername(String username) {
        //根据用户名查询用户具体信息
        User user = userDao.findByUsername(username);
        Integer id = user.getId();
        //依据用户id去查询对一个的roles
        Set<Role> roles = roleDao.findRolesByUserId(id);
        //遍历roles集合
        for (Role role : roles) {
            Integer roleId = role.getId();
            Set<Permission> permissions = permissionDao.findPermissionByRoleId(roleId);
            //将查询到的permissions封装到role对象中
            role.setPermissions(permissions);
        }
        //将查询到的roles封装到user对象中
        user.setRoles(roles);
        return user;
    }
}
